package com.psp.park.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.northend.util.NumUtil;
import com.northend.util.StringUtil;
import com.psp.park.controller.res.BaseResult;
import com.psp.park.controller.res.ListResult;
import com.psp.park.controller.res.ObjectResult;
import com.psp.park.controller.res.bean.RConfirmOrderProductBean;
import com.psp.park.controller.res.bean.ROrderDetailBean;
import com.psp.park.controller.res.bean.ROrderListBean;
import com.psp.park.controller.res.bean.RParkUserFormCooperationRecordListBean;
import com.psp.park.controller.res.bean.RParkUserFormServiceRecordListBean;
import com.psp.park.controller.springmvc.req.GetConfirmOrderProductParam;
import com.psp.park.controller.springmvc.req.GetOrderDetailParam;
import com.psp.park.controller.springmvc.req.GetOrderListParam;
import com.psp.park.controller.springmvc.req.GetParkUserFormRecordListParam;
import com.psp.park.controller.springmvc.req.GetRefuseOrderParam;
import com.psp.park.model.OrderBean;
import com.psp.park.service.JsjService;
import com.psp.park.service.OrderService;
import com.psp.park.service.exception.ServiceException;
import com.psp.park.service.impl.PageResult;

@Component
public class OrderController {
	Logger logger = Logger.getLogger(OrderController.class);

	@Autowired
	OrderService orderServiceImpl;

	@Autowired
	JsjService jsjServiceImpl;

	/**
	 * 园区端业务员--订单列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<ROrderListBean> list(GetOrderListParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<ROrderListBean> result = new ListResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String adminId = (String) request.getAttribute("userId");
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = NumUtil.toInt(param.getPageSize(), 10);
			int state = NumUtil.toInt(param.getState(), 0);
			String orderNo = StringUtil.isEmpty(param.getOrderNo()) ? null : param.getOrderNo();

			PageResult<ROrderListBean> resList = orderServiceImpl.getList(adminId, page, pageSize, orderNo, state);

			result.setData(resList.getData());
			result.setTotalSize(resList.getCount());
			result.setPage(page);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 园区端业务员帮助来访客户下单--协助
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult help(GetRefuseOrderParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId");
			String pid = param.getPid();
			String uid = param.getUid();
			String skuCode = param.getSkuCode();
			String addresseeAid = param.getAddresseeAid();
			String invoiceHeaderId = param.getInvoiceHeaderId(); // 发票头信息id
			String invoiceAid = param.getInvoiceAid(); // 发票邮寄地址id
			OrderBean order = orderServiceImpl.assistOrder(puid, pid, uid, skuCode, addresseeAid, invoiceHeaderId,
					invoiceAid);
			result.setFlag(order != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 获取订单详情
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<ROrderDetailBean> getDetail(GetOrderDetailParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<ROrderDetailBean> result = new ObjectResult<ROrderDetailBean>();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId");
			String oid = param.getOid();
			ROrderDetailBean resBean = orderServiceImpl.getDetail(puid, oid);
			result.setData(resBean);
			result.setFlag(resBean != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 订单确认时--服务订单产品信息
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RConfirmOrderProductBean> getOrderProduct(GetConfirmOrderProductParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RConfirmOrderProductBean> result = new ObjectResult<>();
		String token = (String) request.getAttribute("token");
		try {
			String puid = (String) request.getAttribute("userId");
			String pid = param.getPid();
			String skuCode = param.getSkuCode();

			RConfirmOrderProductBean resdata = orderServiceImpl.getOrderProduct(puid, pid, skuCode);
			result.setFlag(resdata != null);
			result.setData(resdata);
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(e);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result.setServiceException(new ServiceException("error", "发生异常"));
		}
		result.setToken(token);
		return result;
	}

	/**
	 * 需求登记
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RParkUserFormServiceRecordListBean> serviceRecordList(GetParkUserFormRecordListParam param,
			HttpServletRequest request, HttpServletResponse response) {
		ListResult<RParkUserFormServiceRecordListBean> result = new ListResult<>();
		String puid = (String) request.getAttribute("userId");
		int page = param.getPage();
		int pageSize = param.getPageSize();
		if (pageSize == 0) {
			pageSize = 10;
		}
		PageResult<RParkUserFormServiceRecordListBean> resData = jsjServiceImpl.getRecordList(page, pageSize, puid);
		result.setData(resData.getData());
		result.setTotalSize(resData.getCount());
		return result;
	}

	/**
	 * 合作记录
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RParkUserFormCooperationRecordListBean> cooperationRecordList(
			GetParkUserFormRecordListParam param, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RParkUserFormCooperationRecordListBean> result = new ListResult<>();
		String puid = (String) request.getAttribute("userId");
		int page = param.getPage();
		int pageSize = param.getPageSize();
		if (pageSize == 0) {
			pageSize = 10;
		}
		PageResult<RParkUserFormCooperationRecordListBean> resData = jsjServiceImpl.getCooperationRecordList(page,
				pageSize, puid);
		result.setData(resData.getData());
		result.setTotalSize(resData.getCount());
		return result;
	}

}
